/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab-apps.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

#ifndef __dylab_GuiUpdater_h__
#define __dylab_GuiUpdater_h__

//===========================================================================================
#include <list>
#include <dylab/utils/log/Log.h>
#include <dylab/simulation/ComEventListener.h>

//===========================================================================================
namespace dylab {

    class GuiUpdater 
		: public ComEventListener
    {
	public:
		class LogListener
			: public Log::IListener
		{
		private:
			Simulation * mSimulation;
		public:
			LogListener(Simulation * _sim) : mSimulation(_sim) { DYLAB_ASSERT(mSimulation != NULL); }				
			virtual void logMessage(const String & _message);
		};

    public:
        struct Statistics
        {
            uint32_t currFPS, bestFPS, worstFPS, triangleCount, triangleCountMax;
            real_t techniqueTime, integratorTime, collisionTime, renderingTime, inputTime, hapticsTime, eventsTime;

            void resetStatTimes()
                    {  techniqueTime = integratorTime = collisionTime = hapticsTime
                        = renderingTime = inputTime = eventsTime = Math::ZERO; }
        };

    private:
        Simulation * mSimulation;
        Statistics mStats;
		Statistics mAvgStats;
		uint32_t mAvgStatsQueueMaxSize;
		std::list<Statistics> mAvgStatsQueue;
		uint32_t mNoFramesToInitAvgStats;
        uint32_t mUpdateTimeBuffer;

    public:
        GuiUpdater();
        virtual ~GuiUpdater();
        
        virtual String getDescription() const;
        virtual void initialize(Simulation * _parentSimulation);
		virtual void loadFromPropertyTree(const PropertyTree & _pt)
				{ }
        virtual Simulation * getParentSimulation()
                { return mSimulation; }
        virtual const Simulation * getParentSimulation() const
                { return mSimulation; }        
        virtual void processEvent(const GfxEvent & _gfxEvent);

        Statistics & statistics()
                { return mStats; }

		void processGfxStatistics();

    private:        
        void _updateGui();
    };
}
//===========================================================================================
#endif